package com.leyou.search.mq;

import com.leyou.search.service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: wzh
 * @Date: 2019/4/11 11:16
 * @Description: 接受消息，更新索引库
 * 本项目用到MQ的微服务包括：
 *  1）详情页微服务
 *  2）搜索微服务
 *  3）短信微服务
 */
@Component
public class SearchItemListener {

    //监听完，使用这个操作
    @Autowired
    private SearchService searchService;


    /**
     * 监听商品更新，修改的时候，更新索引库
     * @param spuId
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "serch.item.insert.queue",durable = "true"),
            exchange = @Exchange(name = "ly.item.exchange",type = ExchangeTypes.TOPIC),
            key = {"item.insert","item.update"}
    ))

    public void listenIntertOrUpdate(Long spuId){
        if (spuId == null){
            return;
        }
        searchService.createOrUpdateIndex(spuId);

    }


    /**
     *  当商品删除的时候，删除索引库
     * @param spuId
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "serch.item.delete.queue",durable = "true"),
            exchange = @Exchange(name = "ly.item.exchange",type = ExchangeTypes.TOPIC),
            key = {"item.delete"}
    ))

    public void listenDelete(Long spuId){
        if (spuId == null){
            return;
        }
        searchService.createDelete(spuId);

    }
}
